    package org.lai.service.impl;

    import com.github.pagehelper.Page;
    import com.github.pagehelper.PageHelper;
    import com.github.pagehelper.PageInfo;
    import org.lai.Exception.haveStudentException;
    import org.lai.mapper.ClazzsMapper;
    import org.lai.pojo.Clazz;
    import org.lai.pojo.ClazzQueryParam;
    import org.lai.pojo.PageResult;
    import org.lai.service.ClazzsService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.format.annotation.DateTimeFormat;
    import org.springframework.stereotype.Service;

    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    import java.util.List;

    @Service
    public class ClazzsServiceImpl implements ClazzsService {

        @Autowired
        private ClazzsMapper clazzsMapper;

        @Override
        public PageResult<Clazz> list(ClazzQueryParam clazzQueryParam) {
            // 1. 使用分页查询,获取分页数据(PageHelper)
            PageHelper.startPage(clazzQueryParam.getPage(), clazzQueryParam.getPageSize());
            // 2. 调用Mapper接口
            List<Clazz> clazzList = clazzsMapper.list(clazzQueryParam);
            // 3. 封装结果(需要获取clazzList中的endTime和beginTime得到status)
            LocalDate now = LocalDate.now(); // 获取当前日期
            for (Clazz clazz : clazzList) {
                LocalDate beginDate =  clazz.getBeginDate();// 转换为LocalDate对象
                LocalDate endDate = clazz.getEndDate();
                if (now.isBefore(beginDate)) {
                    clazz.setStatus("未开班");
                } else if (now.isAfter(endDate)) {
                    clazz.setStatus("已结课");
                } else {
                    clazz.setStatus("在读中");
                }
            }
            Page<Clazz> result = (Page<Clazz>) clazzList;

            return new PageResult<Clazz>(result.getTotal(),result.getResult());
        }

        /**
         * 添加班级
         * @param clazz
         */
        @Override
        public void addClazzs(Clazz clazz) {
            clazz.setCreateTime(LocalDateTime.now());
            clazz.setUpdateTime(LocalDateTime.now());
            clazzsMapper.insert(clazz);
        }

        @Override
        public Clazz getInfo(Integer id) {
            return clazzsMapper.getInfo(id);
        }

        /**
         * 修改班级数据
         * @param clazz
         */
        @Override
        public void update(Clazz clazz) {
            clazz.setUpdateTime(LocalDateTime.now());
            clazzsMapper.update(clazz);
        }

        /**
         * 删除班级信息
         * @param id
         */
        @Override
        public void delete(Integer id) {
            Integer num = clazzsMapper.getClazzsStudentCount(id);
            if (num > 0) {
                throw new haveStudentException("该班级有学生，不能删除");
            }
            clazzsMapper.deleteById(id);
        }

        @Override
        public List<Clazz> getList() {
            return clazzsMapper.getList();
        }
    }